import 'package:flutter/material.dart';
import "package:get/get.dart";
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../../common/style.dart';
import '../../../components/appbar_widget/appbar_widget.dart';
import '../../../components/loading/svg_loading.dart';
import '../../../components/status/empty.dart';
import '../../../components/status/load_more_state.dart';
import '../../../components/status/not_network.dart';
import '../../../config/global_config.dart';
import '../../../controller/network/network_ctrl.dart';
import '../../../model/bean/community_bean.dart';
import '../../../utils/screenutil/fit.dart';
import 'ctrl/lift_manage_ctrl.dart';

class LiftManage extends StatelessWidget {
  static const _ALL = "全部";
  static const _ALARM = "已告警";

  // 导入控制器
  final liftCtrl = Get.put(LiftManageCtrl());
  final _nc = Get.find<NetworkCtrl>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarWidget('电梯管理').build(context),
      body: Obx(
        () => liftCtrl.isInitComplete.value
            ? Container(
                height: double.infinity,
                child: Stack(
                  children: [
                    Container(
                      margin:
                          EdgeInsets.only(top: fit(_nc.topDistance.value + 86)),
                      child: liftCtrl.MARKSYMBOL.value
                          ? SmartRefresher(
                              footer: CustomFooter(
                                builder: (BuildContext ct, LoadStatus mode) {
                                  Widget body = LoadMoreState(mode);
                                  return Container(child: Center(child: body));
                                },
                              ),
                              enablePullDown: false,
                              enablePullUp: true,
                              controller: liftCtrl.refreshController.value,
                              onLoading: liftCtrl.onLoading,
                              child: ListView.builder(
                                itemCount: liftCtrl.listData.length,
                                itemBuilder: (BuildContext c, int i) => InkWell(
                                  onTap: () => liftCtrl.onLeave(
                                    monitorId: liftCtrl.listData[i].blackBoxNo,
                                    communityName:
                                        liftCtrl.selectedCommunity.value.name,
                                    liftId: liftCtrl.listData[i].id,
                                    liftLocation:
                                        "${liftCtrl.listData[i].buildingNo}${liftCtrl.listData[i].unitNo}${liftCtrl.listData[i].shaft}",
                                  ),
                                  child: Container(
                                    color: Colors.white,
                                    child: Column(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.end,
                                      children: [
                                        Container(
                                          padding: EdgeInsets.only(
                                            left: 20.f,
                                            top: 15.f,
                                            bottom: 15.f,
                                          ),
                                          child: Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceBetween,
                                            children: [
                                              Container(
                                                alignment: Alignment.centerLeft,
                                                width: 380.f,
                                                child: Text(
                                                  "${liftCtrl.listData[i].buildingNo}/${liftCtrl.listData[i].unitNo}/${liftCtrl.listData[i].shaft}",
                                                  softWrap: true,
                                                  textAlign: TextAlign.right,
                                                  overflow:
                                                      TextOverflow.ellipsis,
                                                  style:
                                                      TextStyle(fontSize: 24.f),
                                                ),
                                              ),
                                              Padding(
                                                padding: EdgeInsets.only(
                                                  right: 20.f,
                                                ),
                                                child: Icon(
                                                  Icons.chevron_right,
                                                  size: 35.f,
                                                  color:
                                                      GlobalConfig.assistColor,
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                        SizedBox(
                                          width: 620.f,
                                          height: 1.f,
                                          child: Container(
                                              color: GlobalConfig.borderColor),
                                        ),
                                      ],
                                    ),
                                  ),
                                ),
                              ),
                            )
                          : Container(
                              width: 640.f,
                              height: double.infinity,
                              child: Center(child: Empty()),
                            ),
                    ),

                    // 选项卡、条件选择器
                    Positioned(
                      child: Container(
                        width: 640.f,
                        height: 85.f,
                        margin: EdgeInsets.only(
                          top: fit(_nc.topDistance.value),
                        ),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          boxShadow: GlobalConfig.boxShadow,
                        ),
                        child: Flex(
                          direction: Axis.horizontal,
                          children: [
                            Expanded(
                              flex: 1,
                              child: InkWell(
                                onTap: () async {},
                                child: Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: [
                                    Container(
                                      width: 300.f,
                                      color: Colors.white,
                                      padding: EdgeInsets.only(
                                        left: 20.f,
                                      ),
                                      child: DropdownButtonHideUnderline(
                                        child: DropdownButton(
                                          items: liftCtrl.communityData.value,
                                          value:
                                              liftCtrl.selectedCommunity.value,
                                          hint: Container(
                                            width: 240.f,
                                            child: Text(
                                              "${liftCtrl.selectedCommunity.value != null ? liftCtrl.selectedCommunity.value.name : '请选择小区'}",
                                              softWrap: true,
                                              maxLines: 1,
                                              textAlign: TextAlign.left,
                                              overflow: TextOverflow.ellipsis,
                                              style: TextStyle(
                                                color: GlobalConfig.assistColor,
                                                fontSize: 24.f,
                                              ),
                                            ),
                                          ),
                                          icon: Icon(
                                            Icons.chevron_right,
                                            color: GlobalConfig.assistColor,
                                            size: 35.f,
                                          ),
                                          onChanged: (CommunityBean val) =>
                                              liftCtrl
                                                  .setDropDownSelectedData(val),
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                            SizedBox(
                              width: 1.f,
                              child: Container(
                                height: 85.f,
                                color: GlobalConfig.borderColor,
                              ),
                            ),
                            Expanded(
                              flex: 1,
                              child: Container(
                                width: 640.f,
                                height: 85.f,
                                color: Colors.white,
                                child: Flex(
                                  direction: Axis.horizontal,
                                  children: [
                                    Expanded(
                                      flex: 1,
                                      child: createTabs(_ALL, 0),
                                    ),
                                    SizedBox(
                                      width: 1.f,
                                      child: Container(
                                        height: 60.f,
                                        decoration: BoxDecoration(
                                          color: GlobalConfig.borderColor,
                                          gradient: Style.gradient,
                                        ),
                                      ),
                                    ),
                                    Expanded(
                                      flex: 1,
                                      child: createTabs(_ALARM, 1),
                                    )
                                  ],
                                ),
                              ),
                            )
                          ],
                        ),
                      ),
                    ),

                    // 网络状态
                    Positioned(top: 0, child: NotNetwork()),
                  ],
                ),
              )
            : SVGLoading(),
      ),
    );
  }

  /// 创建头部选项卡
  /// * [name] String 选项卡名称|必填
  /// * [currentStatus] int 当前状态|必填
  Widget createTabs(String name, int currentStatus) {
    return InkWell(
      onTap: () => liftCtrl.onChangeSelectStatus(currentStatus),
      child: Container(
        alignment: Alignment.center,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              width: 100.f,
              height: 85.f,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                border: Border(
                  bottom: BorderSide(
                    width: 2,
                    color: liftCtrl.status.value == currentStatus
                        ? GlobalConfig.theme
                        : Colors.white,
                  ),
                ),
              ),
              child: Text(
                "$name",
                style: TextStyle(
                  fontSize: 26.f,
                  color: liftCtrl.status.value == currentStatus
                      ? GlobalConfig.theme
                      : GlobalConfig.assistColor,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
